package com.novunda.platform.repositories;

import com.novunda.platform.common.repositories.BaseRepository;
import com.novunda.platform.entities.Office;
import com.novunda.platform.entities.base.BaseEntity;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;

import java.util.List;

/**
 * .
 * 功能说明：机构dao
 * @author linfeng
 * @since 2016/9/5
 * @version 1.0
 * @modify FJ
 */
public interface OfficeRepository extends BaseRepository<Office, Long> {

	/**
	 * .
	 *功能说明：删除机构
	 * @param id 机构id
	 * @param likeParentIds 父机构id
	 * @return
	 */
    @Modifying
    @Query("update Office set delFlag='" + BaseEntity.DEL_FLAG_DELETE + "' where id = ?1 or parentIds like ?2")
    public int deleteById(Long id, String likeParentIds);

    /**
     * .
     *功能说明：通过父机构id查询机构
     * @param parentIds 父机构id
     * @return
     */
    public List<Office> findByParentIdsLike(String parentIds);
    
    /**
     * .
     *功能说明：通过机构名查询
     * @param name 机构名
     * @return
     */
    @Query("from Office where name = ?1 and type = 1 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    Office findByName(String name);
    
    /**
     * .
     *功能说明：通过所属机构查询该部门是否存在
     * @param name 部门名
     * @param parentId 父机构id
     * @return
     */
    @Query("from Office where name = ?1 and type = 2 and parent.id = ?2 and delFlag = '" + BaseEntity.DEL_FLAG_NORMAL + "'")
    Office findOfficeByNameAndTypeAndParent(String name, long parentId);

}
